#!/bin/bash
#===============================================================================
# Copyright 2011 zod.yslin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 
# Author: zod.yslin
# Email: 
# File Name: authentication.sh
# Description: 
#   The SSH protocol is recommended for remote login and remote file transfer 
#   which provides confidentiality and security for data exchanged between two 
#   computer systems, through the use of public key cryptography. 
#   The OpenSSH server provides this kind of setup under Linux. 
#   It is installed by default. 
#   This how-to covers generating and using ssh keys for automated usage such as:
#       *Automated Login using the shell scripts.
#       *Making backups.
#       *Run commands from the shell prompt etc.
# Edit History: 
#   2011-09-30    File created.
#===============================================================================

#===============================================================================
#Task: Generating SSH Keys
#===============================================================================

#First, log on to your workstation ( for example log on to workstation called 
#admin.fbsd.nixcraft.org as vivek user). 
#Please refer the following sample setup - You will be log in, on your local system, 
#AS THE USER you wish to make passwordless ssh connections.

# Architecture
 
# Local system(Ubuntu)     Internet        Remote server(FreeBSD)
# zod@ubuntu.desktop    ───────────────>   yslin@freebsd.cs.nctu.edu.tw
#            ssh key based authentication
#Create the cryptographic Key on FreeBSD / Linux / UNIX workstation (zod@ubuntu.desktop), enter:
ssh-keygen -t rsa

#Assign the pass phrase (press [enter] key twice if you don't want a passphrase).
#It will create 2 files in ~/.ssh directory as follows:
#   ~/.ssh/id_rsa : identification (private) key
#   ~/.ssh/id_rsa.pub : public key

#Use scp to copy the id_rsa.pub (public key) to rh9linux.nixcraft.org server
#as authorized_keys2 file, this is know as Installing the public key to server.
scp ~/.ssh/id_rsa.pub yslin@freebsd.cs.nctu.edu.tw:.ssh/authorized_keys2

#From Ubuntu (zod@ubuntu.desktop) workstation login to server:
ssh yslin@freebsd.cs.nctu.edu.tw


#Changing the pass-phrase on (zod@ubuntu.desktop) workstation (if needed):
ssh-keygen -p

#Use of ssh-agent to avoid continues pass-phrase typing
#At Ubuntu workstation type:
ssh-agent $BASH
ssh-add
# Type your pass-phrase

# Now ssh server will not use prompt for the password. 
# Above two commands can be added to your ~/.bash_profile file so that as soon as you login into workstation you can set the agent.

#Deleting the keys hold by ssh-agent
#To list keys, enter:
ssh-add -l

#To delete all keys, enter:
ssh-add -D

#To delete specific key, enter:
ssh-add -d key
